package cn.gov.pbc.gd.credit.user.service.online.impl;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.liu.jbase.PageListVo;
import com.liu.jbase.ResultVo;
import com.liu.tools.DateUtil;
import com.liu.tools.LogUtil;
import com.liu.tools.StrUtil;

import cn.gov.pbc.gd.credit.api.online.IOnlineUserApi;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.ConfConstant;
import cn.gov.pbc.gd.credit.user.service.online.IOnlineUserService;
import cn.gov.pbc.gd.credit.vo.online.OnlineUserVo;
import cn.gov.pbc.gd.credit.vo.user.UserVo;

@Service
public class OnlineUserServiceImpl implements IOnlineUserService {

	private static Logger logger = LoggerFactory.getLogger(OnlineUserServiceImpl.class);
	
	@Autowired
	private IOnlineUserApi onlineUserApi;
	
	@Override
	public ResultVo<?> add(OnlineUserVo vo, HttpServletRequest request) {
		
		ResultVo<?> resultVo = new ResultVo<>();
 		resultVo.setCode(CodeConstant.SUCCESS);
 		try {
 			vo.setCreateTime(new Date());
			onlineUserApi.insert(vo);
		} catch (Exception e) {
			logger.error(LogUtil.msg("OnlineUserServiceImpl:add", "添加在线用户异常"), e);
			resultVo.setCode(CodeConstant.EXCEPTION);
		}
		return resultVo;
	}

	@Override
	public ResultVo<?> update(OnlineUserVo vo, HttpServletRequest request) {
		
		ResultVo<?> resultVo = new ResultVo<>();
 		resultVo.setCode(CodeConstant.SUCCESS); 		
 		try {
			onlineUserApi.findAndUpdate(vo);
		} catch (Exception e) {
			logger.info("修改在线用户vo的值"+vo);
			
			logger.info(LogUtil.msg("OnlineUserServiceImpl:update", "修改在线用户异常"), e.getMessage());
			resultVo.setCode(CodeConstant.EXCEPTION);
		}
		return resultVo;
	}

	@Override
	public ResultVo<PageListVo<OnlineUserVo>> page(UserVo vo, HttpServletRequest request) {
		
		PageListVo<OnlineUserVo> pageListVo = new PageListVo<>();
		pageListVo.setCurrPage(vo.getCurPage());
		pageListVo.setPageSize(vo.getPageSize());

		ResultVo<PageListVo<OnlineUserVo>> resultVo = new ResultVo<>();
		resultVo.setCode(CodeConstant.SUCCESS);

		
		int rangeTime = ConfConstant.sidCacheTime / 60;//分钟

		long startTime = DateUtil.getDateMill(DateUtil.addDate(new Date(), Calendar.MINUTE, -rangeTime));
		
		OnlineUserVo onlineUserVo = new OnlineUserVo();
		onlineUserVo.setLastActiveTime(startTime);  
		try {
			PageListVo<OnlineUserVo> pageList = onlineUserApi.getPage(onlineUserVo, pageListVo);
			resultVo.setData(pageList);
		} catch (Exception e) {
			logger.error(LogUtil.msg("OnlineUserServiceImpl:page", "查询在线用户异常"), e);
			resultVo.setCode(CodeConstant.EXCEPTION);			
		}
		
		return resultVo;
	}

	@Override
	public ResultVo<String> userOffline(OnlineUserVo vo, HttpServletRequest request) {
		
		ResultVo<String> resultVo = new ResultVo<>();
		
		if(vo == null || StrUtil.isNull(vo.getSessionId())){
			
			resultVo.setCode(CodeConstant.REQ_PARAM_FAIL);
			resultVo.setMsg("请求参数不正确");
			logger.error(LogUtil.msg("LoginServiceImpl:userOffline", "请求参数不正确:" + JSON.toJSONString(vo)));
			return resultVo;
		}
		

		int rangeTime = ConfConstant.sidCacheTime / 60;//分钟
		vo.setLastActiveTime(DateUtil.getDateMill(DateUtil.addDate(new Date(), Calendar.MINUTE, -rangeTime)));
		logger.info(LogUtil.msg("LoginServiceImpl:userOffline", "用户下线更新:" + JSON.toJSONString(vo)));
		try {
			onlineUserApi.findAndUpdate(vo);
		} catch (Exception e) {
			resultVo.setCode(CodeConstant.EXCEPTION);
			resultVo.setMsg("用户下线失败");
			logger.error(LogUtil.msg("LoginServiceImpl:userOffline", "用户下线异常:" + JSON.toJSONString(vo)), e);
		}
		resultVo.setCode(CodeConstant.SUCCESS);
		return resultVo;
	}

}
